package com.jaychen.tliaswebmanagement.exception;

import com.jaychen.tliaswebmanagement.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /*@ExceptionHandler
    public Result handleException(final Exception e) {
        log.error("程序出错啦~", e);
        return Result.error("出错啦，请联系管理员~");
    }*/

    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException e) {
        log.error("程序出错啦~", e);
        String errorMessage = e.getMessage();
        int index = errorMessage.indexOf("Duplicate entry");
        String errorMsg = errorMessage.substring(index);
        String[] arr = errorMsg.split(" ");
        return Result.error(arr[2] + "已存在");
    }
}
